import switchTab from "../commonComponents/switchTab.vue"
import templateItems from "../commonComponents/templateItems.vue"
import store from "../../store/store";

export default {
  data() {
    return {
      type: 0,
      switchFlag: 0,
      draftFlag: false,
      isPush: false,
      dPath: "/infoSubmitList/infoSubmit/",
      typeArr: [],
      submitList: [
        {
          title: '基本信息',
          list: []
        },
        {
          title: '扩展信息',
          list: []
        }
      ],
    }
  },
  methods: {
    init() {
      _data.effectDataGet = {};
      this.isPush = parseInt(_methods.getUrlParam("isPush"));
      this.id = parseInt(_methods.getUrlParam("id"));
      this.infoId = parseInt(_methods.getUrlParam("infoId"));
      this.typeReset = parseInt(_methods.getUrlParam("typeReset"));
      _methods.headerTitleChange(101);
      _methods.headerRightShow();
      this.draftFlag = _methods.getUrlParam("isDraft");
      this.getDom();
      if (this.draftFlag == 1) {
        this.getDetail();
      } else {
        this.getTem();
      }
    },
    getDom() {
      this.submitList[0].list = [
        {
          "attributesValue": "",
          "element": "text",
          id: 'title',
          singLine: 1,
          isRequired: 1,
          "label": "标题",
          "placeholder": "请输入标题",
        },
        {
          "attributesValue": _methods.formatDate(),
          "element": "datetime",
          singLine: 1,
          endDate: new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate(),new Date().getHours(),new Date().getMinutes()),
          isRequired: 1,
          id: 'happenTime',
          "label": "发生日期",
        },
        {
          "attributesValue": window._data.userInfo.deptName,
          "element": "text",
          singLine: 1,
          id: '',
          isReadonly: true,
          "label": "报送单位",
        },
        {
          "attributesValue": window._data.userInfo.name,
          "element": "text",
          id: '',
          singLine: 1,
          isReadonly: true,
          "label": "填报人",
        },
        {
          "attributesValue": window._data.userInfo.phoneNumber,
          "element": "text",
          singLine: 1,
          id: '',
          isReadonly: true,
          "label": "联系电话",
        },
        {
          "attributesValue": window._data.userInfo.phone,
          "element": "text",
          singLine: 1,
          id: 'fax',
          isReadonly: false,
          "placeholder": "请输入传真",
          "label": "传真",
        },
      ];
    },
    getTem() {
      _ajax.noLoading(_ajaxUrls.addTemList.url + "/" + this.id,
        {isFeedback: 2},
        (ajData) => {
          let data = ajData.result;
          this.submitList[1].list = _methods.temFormat(data);
        }, _ajaxUrls.addTemList.type);
    },
    baseInfoGet() {
      // sendUserId sendDeptId happenDate
      const sVal = (id, txt, val) => {
        let el = document.getElementById(id);
        if (el) {
          el.value = txt;
          el.setAttribute("data-value", val);
        }
      };
      let time = _methods.formatDate(new Date(), 'yyyy-MM-dd');
      // sVal("happenDate", time, time);
      _methods.sVal("phone", _data.userInfo.phone, _data.userInfo.phone);
      if (this.type === 5) {
        _methods.setValue("title", `${_data.userInfo.deptName} 快速上报`);
      }
      // sVal("sendDeptId", "报送单位", "、id");
    },
    getDetail() {
      let id = _methods.getUrlParam("infoId");
      // 1 草稿箱详情
      _ajax.withLoading(_ajaxUrls.addInfoList.url + "/" + id,
        {},
        (ajData) => {
          let data = ajData.result;
          this.submitList[1].list = _methods.temLoad(data.extend);
          this.submitList[0].list[0].attributesValue = data.title;
          this.submitList[0].list[5].attributesValue = data.fax;
        }, _ajaxUrls.addInfoList.type);
    },
    btnClick(type) {
      // type 1提交 2 保存
      let isEnabled = ["", 1, 4][type];
      let ajType = "post";
      let url = _methods.copyObj(_ajaxUrls.addInfoList);
      if (this.draftFlag == 1) {
        url.url = url.url + "/" + this.infoId;
        ajType = "put";
      }
      let flag = _methods.checkRequired(this.submitList, type);
      if (flag) {

        let fax = _methods.getValue('fax');
        let isFax = _filterData.isFax(fax);
        if (fax && !isFax) {
          mui.toast("传真填写错误");
          return;
        }
        _alert.confirm(()=>{
          let list = _methods.temSubmit(this.submitList[1].list);
          let data = {
            isEnabled,
            happenTime: _methods.getValue('happenTime'),
            title: _methods.getValue('title'),
            fax,
            templateID: this.id,
            attributeBeanJSON: JSON.stringify(list),
          };
          _ajax.withLoading(url.url,
            data,
            () => {
              _methods.meScrollTriggerDownScrollArr([_data.submitList, _data.draftList]);
              _alert.alert(() => {
                _methods.goBack();
              }, "提交成功");
            },
            ajType);
        });

      }
    },
  },
  components: {
    switchTab,
    templateItems,
  },
  created() {
    this.init();
  },
  mounted() {
    this.baseInfoGet();
  },
  beforeDestroy() {
    _methods.headerTitleChange(1, true);
    _methods.headerRightShow({
      val: 999,
      txt: ""
    });
  }
}
